package ${packageName}.web.modules.${moduleName}.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.mdc.mansion.web.modules.system.utils.UserUtils;
import com.mdc.mansion.dataaccess.modules.system.entity.User;

import com.mdc.mansion.dataaccess.common.persistence.Page;
import ${packageName}.dataaccess.modules.${moduleName}.entity.${ClassName};
import ${packageName}.core.modules.${moduleName}.${ClassName}Service;
import ${packageName}.web.common.web.BaseController;
/**
 * ${functionName}Controller
 * @author ${classAuthor}
 * @version ${classVersion}
 */
@Controller
@RequestMapping(value = "${r"${adminPath}"}/${urlPrefix}")
public class ${ClassName}Controller extends BaseController {

	@Autowired
	private ${ClassName}Service ${className}Service;
	
	@ModelAttribute
	public ${ClassName} get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return ${className}Service.get(id);
		}else{
			return new ${ClassName}();
		}
	}
	
	@RequiresPermissions("${permissionPrefix}:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(${ClassName} ${className}) {
		Map<String, Object> model=new HashMap<String, Object>();
		Page<${ClassName}> page = ${className}Service.findPage(${className});
        model.put("page", page);
		ModelAndView modelAndView=new ModelAndView("modules/${viewPrefix}List", model);
		return modelAndView;
	}
	
	@RequiresPermissions("${permissionPrefix}:view")
	@RequestMapping(value = "form")
	public ModelAndView form(${ClassName} ${className}) {
		Map<String, Object> model=new HashMap<String, Object>();
		model.put("${className}", ${className});
		return new ModelAndView("modules/${viewPrefix}Form",model);
	}
	

	@RequiresPermissions("${permissionPrefix}:edit")
	@RequestMapping(value = "save")
	public ModelAndView save(${ClassName} ${className},RedirectAttributes redirectAttributes) {
		User u=UserUtils.getUser();
		${className}.setUpdateBy(u);
		if (StringUtils.isNotBlank(${className}.getId())){
			${className}Service.updateSelective(${className});
		}else{
			${className}.setCreateBy(u);
			${className}Service.save(${className});
		}
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	@RequiresPermissions("${permissionPrefix}:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(${ClassName} ${className},RedirectAttributes redirectAttributes) {
		${className}Service.delete(${className});
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
}
